#region License

/*
    Copyright (c) 2012 Joseph Haluska
    This source is subject to the Microsoft Public License (Ms-PL).
    Please see http://www.microsoft.com/en-us/openness/licenses.aspx for details.
    All other rights reserved.
*/

#endregion License

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace StudioControls.Controls
{
    public class WarningBox : Control
    {
        #region Fields

        private TextBox label;

        private HatchBrush hatchBrush;

        private SolidBrush backColorSolidBrush;

        private SolidBrush foreColorSolidBrush;

        #endregion Fields

        #region Properties

        #region BackColor Color property

        /// <summary>
        /// Gets or sets the background color of the control.
        /// </summary>
        [
        Category("Appearance"),
        Description("Gets or sets the background color of the control."),
        DefaultValue(typeof(SystemColors), "Control")
        ]
        public override Color BackColor
        {
            get { return base.BackColor; }
            set
            {
                base.BackColor = value;
                label.BackColor = value;
                hatchBrush = new HatchBrush(HatchStyle.WideUpwardDiagonal, base.ForeColor, base.BackColor);
                backColorSolidBrush = new SolidBrush(base.BackColor);
                this.Refresh();
            }
        }

        #region serialization

        private bool ShouldSerializeBackColor()
        {
            // Only serialize nondefault values
            return (base.BackColor != SystemColors.Control);
        }

        /// <summary>
        /// Resets BackColor property.
        /// </summary>
        public override void ResetBackColor()
        {
            base.BackColor = SystemColors.Control;
            label.BackColor = SystemColors.Control;
            hatchBrush = new HatchBrush(HatchStyle.WideUpwardDiagonal, base.ForeColor, base.BackColor);
            backColorSolidBrush = new SolidBrush(base.BackColor);
            this.Refresh();
        }

        #endregion serialization

        #endregion BackColor Color property

        #region ForeColor Color property

        /// <summary>
        /// Gets or sets the background color of the control.
        /// </summary>
        [
        Category("Appearance"),
        Description("The foreground color of this component, which is used to display text."),
        DefaultValue(typeof(SystemColors), "ControlText")
        ]
        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                base.ForeColor = value;
                hatchBrush = new HatchBrush(HatchStyle.WideUpwardDiagonal, base.ForeColor, base.BackColor);
                foreColorSolidBrush = new SolidBrush(base.ForeColor);
                this.Refresh();
            }
        }

        #region serialization

        private bool ShouldSerializeForeColor()
        {
            // Only serialize nondefault values
            return (base.ForeColor != SystemColors.ControlText);
        }

        /// <summary>
        /// Resets ForeColor property.
        /// </summary>
        public override void ResetForeColor()
        {
            base.ForeColor = SystemColors.ControlText;
            foreColorSolidBrush = new SolidBrush(base.ForeColor);
            hatchBrush = new HatchBrush(HatchStyle.WideUpwardDiagonal, base.ForeColor, base.BackColor);
            this.Refresh();
        }

        #endregion serialization

        #endregion ForeColor Color property

        #region Text string property

        /// <summary>
        /// Gets or sets the text for this item.
        /// </summary>
        [
        Description("Gets or sets the text for this item."),
        DefaultValue(""),
        Browsable(true)
        ]
        public new string Text
        {
            get
            {
                return base.Text;
            }

            set
            {
                base.Text = value;
                label.Text = value;
                this.Refresh();
            }
        }

        #endregion Text string property

        #endregion Properties

        #region Constructor / Destructor

        public WarningBox()
        {
            this.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer,
                true);

            hatchBrush = new HatchBrush(HatchStyle.WideUpwardDiagonal, base.ForeColor, base.BackColor);

            backColorSolidBrush = new SolidBrush(base.BackColor);
            foreColorSolidBrush = new SolidBrush(base.ForeColor);

            label = new TextBox();
            label.BorderStyle = BorderStyle.None;
            label.Multiline = true;
            label.ScrollBars = ScrollBars.Both;

            //label.AutoEllipsis = true;

            this.Controls.Add(label);
        }

        ~WarningBox()
        {
            if (hatchBrush != null)
                hatchBrush.Dispose();

            if (backColorSolidBrush != null)
                backColorSolidBrush.Dispose();

            if (foreColorSolidBrush != null)
                foreColorSolidBrush.Dispose();
        }

        #endregion Constructor / Destructor

        #region Members

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle hatchBounds = new Rectangle(0, 0, Width, Height);

            // fill center with hatch marks
            e.Graphics.FillRectangle(hatchBrush, hatchBounds);

            //TODO: make this a property for hatch thickness
            //hatchBounds.Inflate(-10, -10);

            hatchBounds.Inflate(-2, -2);

            // erase hatch marks from center area
            e.Graphics.FillRectangle(backColorSolidBrush, hatchBounds);

            //hatchBounds.Inflate(-13, -13);
            //e.Graphics.DrawString(this.Text, this.Font, foreColorSolidBrush, hatchBounds, StringFormat.GenericTypographic);

            //TODO: adjust the textbox's location

            label.Location = hatchBounds.Location;
            label.Size = hatchBounds.Size;

            // Padding is used to erase the outer border of hash marks.

            Rectangle padding;

            if (this.Padding.Left > 0)
            {
                padding = new Rectangle(0, 0, this.Padding.Left, Height);
                e.Graphics.FillRectangle(backColorSolidBrush, padding);
            }

            if (this.Padding.Right > 0)
            {
                padding = new Rectangle(Width - this.Padding.Right, 0, Width - this.Padding.Right, Height);
                e.Graphics.FillRectangle(backColorSolidBrush, padding);
            }

            if (this.Padding.Top > 0)
            {
                padding = new Rectangle(0, 0, Width, this.Padding.Top);
                e.Graphics.FillRectangle(backColorSolidBrush, padding);
            }

            if (this.Padding.Bottom > 0)
            {
                padding = new Rectangle(0, Height - this.Padding.Bottom, Width, Height - this.Padding.Bottom);
                e.Graphics.FillRectangle(backColorSolidBrush, padding);
            }

            base.OnPaint(e);
        }

        #endregion Members
    }
}